package interceptors

import (
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
	"gpt-api/framework/ginplus"
)

// Cookie pc设置cookie
func Cookie() gin.HandlerFunc {
	return func(c *gin.Context) {
		platform := ginplus.GetPlatform(c)
		if platform == ginplus.PCPlatform || platform == ginplus.H5Platform {
			ck, _ := c.Cookie("fid")
			if ck == "" && c.Request.Method == http.MethodGet {
				domain := strings.TrimPrefix(strings.TrimPrefix(c.Request.Host, "http://"), "https://")
				u := uuid.NewV4()
				c.SetSameSite(http.SameSiteNoneMode)
				c.SetCookie("fid", u.String(), 3600*24*365, "/", domain, true, false)
			}
		}
	}
}
